

jQuery(function ($) {
    var user_info = JSON.parse(localStorage.getItem('user_info'));
    if (!user_info) {
        window.location.href = "/login/login.html";
    }
    var id = user_info.id;
    var currnt_point = user_info.growth;
    var param = {
        "YcUser": {
            "id": id
        },
        "UserLevel[]": {
            "UserLevel": {
                "@order": "bottom_point"
            }
        }
    };
    var html = "";
    var result = [];
    if (id) {
        send_ajax('post', '/get', param, function callback(data) {
            if (data.code == 200) {
                var current_index = 0;
                var level_array = data["UserLevel[]"];
                for (var i = 0; i < level_array.length; i++) {
                    var a_level = level_array[i];
                    //判断当前是属于哪个等级
                    if (currnt_point < a_level.bottom_point) {
                        //说明此等级为下一等级
                        current_index = i;
                        break;
                    }
                };

                //获取当前等级和下一等级
                var currnt_level = level_array[i - 1];
                result.push(data.YcUser);
                for (var i = 0; i < result.length; i++) {
                    html += "\n                            <li>\n                                <div class=\"pic\">\n                                    <img src=\"" + (result[i].portrait ? isImgHttp(result[i].portrait) : '/asset/image/head.png') + "\" alt=\"\" class=\"head_img\">\n                                    <input id=\"upload\" name=\"file\" accept=\"image/*\" type=\"file\" style=\"display: none\">\n                                    <span class=\"name\">" + (result[i].uid ? result[i].uid : '') + "</span>\n                                </div>\n                            </li>\n                            <li>\n                                <span class=\"label\">\u767B\u5F55\u540D\uFF1A</span>\n                                <span id=\"login_name\">" + (result[i].login_name ? result[i].login_name : '') + "</span>\n                                <input type=\"text\" class=\"itxt loginName hide\" value=\"" + (result[i].login_name ? result[i].login_name : "") + ("\">\n                                <i id=\"pen\"></i>\n                            </li>\n                                <li>\n                                <span class=\"label\">\u6635\u79F0\uFF1A</span>\n                                <input type=\"text\" class=\"itxt nick_name\" value=\"") + (result[i].nickname ? result[i].nickname : "") + "\">\n                            </li>\n                            <li>\n                                <span class=\"label\">\u771F\u5B9E\u59D3\u540D\uFF1A</span>\n                                <input type=\"text\" class=\"itxt real_name\" value=\"" + (result[i].real_name ? result[i].real_name : "") + ("\">\n                            </li>\n                            <li>\n                                <span class=\"label\">\u51FA\u751F\u65E5\u671F\uFF1A</span>\n                                <input type=\"text\" class=\"year txt_modify\" placeholder=\"\u5E74\" maxlength=\"4\" value=\"" + (result[i].birthday ? conversionDate(result[i].birthday).split('-')[0] : '') + "\">\n                                <em>-</em>\n                                <input type=\"text\" class=\"month txt_modify\" placeholder=\"\u6708\" maxlength=\"2\" value=\"" + (result[i].birthday ? conversionDate(result[i].birthday).split('-')[1] : '') + "\">\n                                <em>-</em>\n                                <input type=\"text\" class=\"day txt_modify\" placeholder=\"\u65E5\" maxlength=\"2\" value=\"" + (result[i].birthday ? conversionDate(result[i].birthday).split('-')[2] : '') + "\">\n                            </li>\n                            <li>\n                                <span class=\"label\">\u6027\u522B\uFF1A</span>\n                                <input type=\"radio\" name=\"sex\" class=\"ycradio\" value=\"\u7537\" ") + (result[i].sex == 1 ? 'checked' : "") + ">\n                                <label >\u7537</label>\n                                <input type=\"radio\" name=\"sex\" class=\"ycradio\" value=\"\u5973\" " + (result[i].sex == 0 ? 'checked' : "") + ">\n                                <label >\u5973</label>\n                            </li>\n                            <li>\n                                <span class=\"label\">\u90AE\u7BB1\uFF1A</span>\n                                <strong class=\"" + (result[i].email ? 'open' : 'hide') + " email_modify\">\n                                        <span>" + result[i].email + "</span>\n                                        <a class=\"modify\">\u4FEE\u6539</a>\n                                        <span class=\"h_v\" class=\" \">\u5DF2\u9A8C\u8BC1</span>\n                                </strong>\n                            <strong class=\"" + (result[i].email ? 'hide' : 'open') + " email_testing\">\n                                    <input type=\"text\" class=\"itxt \" value=\"" + (result[i].email ? result[i].email : '') + "\">\n                                    <a class=\"verify_now\">\u7ACB\u5373\u9A8C\u8BC1</a>\n                            </strong>\n                            </li>\n                            <li>\n                                <span class=\"label\">\u624B\u673A\u53F7\uFF1A</span>\n                                <strong class=\"" + (result[i].phone ? 'open' : 'hide') + " phone_modify\">\n                                    <span>" + result[i].phone + "</span>\n                                    <a id=\"phone_modify\">\u4FEE\u6539</a>\n                                    <span class=\"h_v\" class=\"" + (result[i].phone != undefined ? 'open' : 'hide') + "\">\u5DF2\u9A8C\u8BC1</span>\n                                </strong>\n                                <strong class=\"" + (result[i].phone ? 'hide' : 'open') + " phone_testing\">\n                                        <input type=\"text\" class=\"itxt \" value=\"\">\n                                        <a class=\"verify_now\">\u7ACB\u5373\u9A8C\u8BC1</a>\n                                </strong>\n                                \n                            </li>\n                            <li>\n                                <span class=\"label\"></span>\n                                <a class=\"confirm_btn\">\u786E\u8BA4\u4FEE\u6539</a>\n                            </li>\n                            ";
                }
                $('.user_info_list').html(html);
                //修改登录名
                $('#pen').click(function () {
                    $('.loginName').removeClass('hide').addClass('open');
                    $('#login_name').addClass('hide').removeClass('open');
                    $(this).hide();
                });
                // $('.loginName').click(function(){
                //     $('#login_name').removeClass('hide').addClass('open');
                //     $('.loginName').addClass('hide').removeClass('open');
                //     $('#pen').show();
                // })
                //禁止输入中文
                // var re = /[\u4E00-\u9FA5]|[\uFE30-\uFFA0]/g
                $('.txt_modify').on('input', function () {
                    var value = $(this).val();
                    var re = /[\u4E00-\u9FA5]|[\uFE30-\uFFA0]/g;
                    this.value = value.replace(re, '');
                });
                $('.modify').click(function () {
                    $(this).parent().removeClass('open').addClass('hide');
                    $('.email_testing').addClass('open').removeClass('hide');
                });
                $('#phone_modify').click(function () {
                    $(this).parent().removeClass('open').addClass('hide');
                    $('.phone_testing').addClass('open').removeClass('hide');
                });
                //提交修改
                $('.confirm_btn').click(function () {
                    var real_name = $('.real_name').val();
                    var nick_name = $('.nick_name').val();
                    var birthday = $('.year').val() + "-" + $('.month').val() + "-" + $('.day').val();
                    var sex = $("input[type='radio']:checked").val();
                    var login_name = $(".loginName").val();
                    var sex_num = 1;
                    if (sex == '男') {
                        sex_num = 1;
                    } else {
                        sex_num = 0;
                    }
                    var params = {
                        "YcUser": {
                            "id": id,
                            "nickname": nick_name,
                            "real_name": real_name,
                            "sex": sex_num,
                            "birthday": birthday
                        },
                        "tag": "YcUser"
                    };
                    send_ajax('post', '/put', params, function callback(data) {});
                });
            }
        });
    };

    if (id) {
        $(document).on('click','.head_img',function(){
            $("#upload").click(); //隐藏了input:file样式后，点击头像就可以本地上传
            $("#upload").on("change", function () {
                var formData = new FormData();
                formData.append("image", $("input[name='file']")[0].files[0]);
                $.ajax({
                    url: host + "/api/upload/img/2",
                    type: 'POST',
                    data: formData,
                    cache: false,
                    // // 告诉jQuery不要去处理发送的数据
                    processData: false,
                    // 告诉jQuery不要去设置Content-Type请求头
                    contentType: false,
                    beforeSend: function beforeSend() {},
                    success: function success(data) {
                        if(data.code==200){
                            alert('上传成功')
                            var param={
                                "YcUser": {
                                    "id": id,
                                    "portrait": data.data
                                }
                            };
                            send_ajax('post', '/put', param, function callback(data) {
                            });
                        }
                    },
                    error: function error(data) {
                        alert('网络加载失败，请稍候再试!');
                    }
                });
            });
        });
    };
    function conversionDate(data) {
        var time = new Date(data);
        var birthday = time.getFullYear() + "-" + (time.getMonth() + 1 < 10 ? '0' + (time.getMonth() + 1) : time.getMonth() + 1) + "-" + (time.getDate() < 10 ? '0' + time.getDate() : time.getDate());
        return birthday;
    }
});